<?php
/**
 * Created by PhpStorm.
 * User: Kevin
 * Date: 2018/2/1
 * Time: 11:08
 */
namespace app\admin\Controller;
use think\Db;

class SubjectController extends CommonController{
    /**
     * getSubjectData 获取课程信息
     * @return 返回json格式数据
     */
    public function getSubjectData(){
        $rows = $_POST['rows'];
        $page = $_POST['page'];
        $where = $this->getDataByCampusid($_POST);
        $res = Db::table('ew_subject')
            ->where($where)
            ->limit($rows * ($page - 1), $rows)
            ->select();

        $total = Db::table('ew_subject')
            ->where($where)
            ->count();
        $data['total'] = $total;
        $data['rows'] = $res;
        return json_encode($data);
    }

    /**
     * addSubject 课程信息添加页面
     * @return 返回HTML页面
     */
    public function addSubject(){
        return $this->fetch('subject/addSubject');
    }

    /**
     * insert 收集课程信息添加页面数据，加入数据库
     * @return 返回数据库操作状态
     */
    public function insert(){
        /*课程名*/
        if($_POST['s_name'] == ''){
            $returnData['status'] = 0;
            $returnData['msg'] = "请填写课程名！";
            return json_encode($returnData);
        }

        $res = Db::table('ew_subject')
            ->insert(['s_name'=>$_POST['s_name']]);
        if($res){
            $returnData['status'] = 1;
            $returnData['msg'] = "添加成功！";
            return json_encode($returnData);
        }else{
            $returnData['status'] = 0;
            $returnData['msg'] = "添加失败！";
            return json_encode($returnData);
        }
    }


    /**
     * updateSubject 展示课程修改页面
     * @return 返回HTML页面
     */
    public function updateSubject(){
        $data = Db::table('ew_subject')->where('s_id',$_POST['nid'])->find();
        $this->assign('res',$data);
        return $this->fetch('subject/updateSubject');
    }

    /**
     * update 收集课程修改页面数据，修改数据库
     * @return 返回修改数据库状态
     */
    public function update(){
        /*课程名*/
        if($_POST['s_name'] == ''){
            $returnData['status'] = 0;
            $returnData['msg'] = "请填写课程名！";
            return json_encode($returnData);
        }

        $res = Db::table('ew_subject')
            ->where('s_id',$_POST['s_id'])
            ->update(['s_name'=>$_POST['s_name']]);
        if($res){
            $returnData['status'] = 1;
            $returnData['msg'] = "修改成功！";
            return json_encode($returnData);
        }else{
            $returnData['status'] = 0;
            $returnData['msg'] = "修改失败！";
            return json_encode($returnData);
        }
    }

    /**
     * deleteSubject 删除课程
     * @return 返回修改数据库状态
     */
    public function deleteSubject(){
        $res = Db::table('ew_subject')
            ->delete($_POST['s_id']);
        if($res){
            $returnData['status'] = 1;
            $returnData['msg'] = "删除成功！";
            return json_encode($returnData);
        }else{
            $returnData['status'] = 0;
            $returnData['msg'] = "删除失败！";
            return json_encode($returnData);
        }
    }
}